package com.example.demo.controller;

import com.example.demo.mapper.BookDao;
import com.example.demo.service.BookSeriver;
import com.example.demo.student.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author :  DongZheng Wang
 * @PROJECT_NAME: redistogether
 * @date :  2022-12-07 18:56
 **/
@RestController
public class BookController {
    @Autowired
 private BookSeriver bookSeriver;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @GetMapping("/books")
    public List<Book> test1() throws Exception {
        ValueOperations<String, Object> forValue = redisTemplate.opsForValue();
        //1. 查询缓存：key的名称：{模块名称::类名::唯一主键}
        List<Book> o = (List<Book>) forValue.get("book::" + Book.class.newInstance().getIsbn());
        if (o != null){
            return o;
        }
        //有缺点就是他会随着时间缓存会越来越大
        List<Book> list = bookSeriver.list();
        if (list != null){
            forValue.set("book::" + Book.class.newInstance().getIsbn(),list,3600, TimeUnit.SECONDS);
        }
        return list;
    }
    @GetMapping("/books1")
    public Book test2(Integer id){
        Book byId = bookSeriver.getById(id);
        return byId;
    }

    @PostMapping("/bookstwo")
    public boolean test3(@RequestBody Book book){
        boolean save = bookSeriver.save(book);
       return save;
    }

    @GetMapping("/delete")
    public boolean test4(Integer id){
        //删除缓存中的指定key
        redisTemplate.delete("book"+id);
        boolean remove = bookSeriver.removeById(id);
        return remove;
    }
    @PostMapping("/update")
    public boolean test4(@RequestBody Book book){
        //只是修改数据库，但是缓存的数据还是之前的
        boolean b = bookSeriver.saveOrUpdate(book);
        ValueOperations<String, Object> forValue = redisTemplate.opsForValue();
        forValue.set("book::"+book.getIsbn(),book,3600, TimeUnit.SECONDS);
        return b;
    }

    @GetMapping("/delete1")
    @CacheEvict(cacheNames = "redis::book",key="#id")
    public boolean test5(Integer id){
        boolean remove = bookSeriver.removeById(id);
        return remove;
    }

    @GetMapping("/books3")
    @Cacheable(cacheNames = "redis::book",key="#id")
    public Book test6(Integer id) {
        Book list = bookSeriver.getById(id);
        return list;
    }

    @PostMapping("/update1")
    @CachePut(cacheNames = "redis::book",key = "#book.isbn")
    public boolean test6(@RequestBody Book book){
        boolean b = bookSeriver.saveOrUpdate(book);
        return b;
    }
}

